package com.sxg.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxg.convert.SxgStockConvert;
import com.sxg.dao.SxgStockDao;
import com.sxg.entity.SxgStockEntity;
import com.sxg.query.SxgStockQuery;
import com.sxg.service.SxgStockService;
import com.sxg.vo.SxgStockVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sxg.page.PageResult;

import java.util.List;

/**
 * 商品库存
 *
 * @author 浪漫写尽歌谱 3298751420@qq.com
 * @since 1.0.0 2024-03-07
 */
@Service
@AllArgsConstructor
public class SxgStockServiceImpl extends ServiceImpl<SxgStockDao, SxgStockEntity> implements SxgStockService {

    @Override
    public PageResult<SxgStockVO> page(SxgStockQuery query) {
        IPage<SxgStockEntity> page = baseMapper.selectPage(PageResult.getPage(query), getWrapper(query));

        return new PageResult<>(SxgStockConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<SxgStockEntity> getWrapper(SxgStockQuery query){
        LambdaQueryWrapper<SxgStockEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(SxgStockVO vo) {
        SxgStockEntity entity = SxgStockConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(SxgStockVO vo) {
        SxgStockEntity entity = SxgStockConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}